﻿using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FCNS.Windows
{
    /// <summary>
    /// 键盘模拟事件
    /// </summary>
    public static class KeyboardSimulator
    {
        /// <summary>
        /// 按下键盘按键
        /// </summary>
        /// <param name="key"></param>
        public static void KeyDown(Keys key)
        {
            WinApi.keybd_event(ParseKey(key), 0, 0, 0);
        }
        /// <summary>
        /// 释放键盘按键
        /// </summary>
        /// <param name="key"></param>
        public static void KeyUp(Keys key)
        {
            WinApi.keybd_event(ParseKey(key), 0, WinApi.KEYEVENTF_KEYUP, 0);
        }
        /// <summary>
        /// 按下并释放键盘按键
        /// </summary>
        /// <param name="key"></param>
        public static void KeyPress(Keys key)
        {
            KeyDown(key);
            KeyUp(key);
        }
        /// <summary>
        /// 模拟标准按键
        /// </summary>
        /// <param name="shortcut"></param>
        public static void SimulateStandardShortcut(StandardShortcut shortcut)
        {
            switch (shortcut)
            {
                case StandardShortcut.Copy:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.C);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Cut:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.X);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Paste:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.V);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.SelectAll:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.A);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Save:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.S);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Open:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.O);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.New:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.N);
                    KeyUp(Keys.Control);
                    break;
                case StandardShortcut.Close:
                    KeyDown(Keys.Alt);
                    KeyPress(Keys.F4);
                    KeyUp(Keys.Alt);
                    break;
                case StandardShortcut.Print:
                    KeyDown(Keys.Control);
                    KeyPress(Keys.P);
                    KeyUp(Keys.Control);
                    break;
            }
        }

        /// <summary>
        /// 通用标准键盘快捷键
        /// </summary>
        public enum StandardShortcut
        {
            Copy,
            Cut,
            Paste,
            SelectAll,
            Save,
            Open,
            New,
            Close,
            Print
        }

        static byte ParseKey(Keys key)
        {
            switch (key)
            {
                case Keys.Alt:
                    return (byte)18;
                case Keys.Control:
                    return (byte)17;
                case Keys.Shift:
                    return (byte)16;
                default:
                    return (byte)key;
            }
        } 
    }
}
